package com.shujia.flink.tf;

//import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo3Filter {
    public static void main(String[] args) throws Exception {
        //1、创建环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //2、读取数据
        DataStream<String> studentDS = env.readTextFile("data/students/csv");

        //3、取出年龄打打印23的学生
/*        DataStream<String> filterDS = studentDS.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String stu) throws Exception {
                String[] split = stu.split("\\|");
                int age = Integer.parseInt(split[2]);
                return age > 23;
            }
        });*/


        //lambda 表达式
        DataStream<String> filterDS2 = studentDS.filter(stu -> {
            String sex = stu.split("\\|")[3];
            //equals:比较两个对象是否一样
            //将需要比较的字符串放在前面是为了避免出现空指针异常
            return "男".equals(sex);
        });

        filterDS2.print();

        env.execute();

    }
}
